package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.SmsExcution;
import com.fortune.dto.in.sms.SmsReSetPassWordDto;
import com.fortune.enums.SmsEnum;
import com.fortune.service.SmsService;
import com.fortune.util.RandomCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "wechat/sms", description = "小程序重置密码", tags = {"微信小程序重置密码"})
@RestController
@RequestMapping("wechat/sms")
public class WechatSmsController {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private SmsService smsService;

    @ApiOperation(value = "发送重置密码短信", nickname = "sendResetPasswordSms", notes = "", tags={ "@郭阳" })
    @PostMapping(value = "code/resetPassword",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> sendResetPasswordSms(@Validated @RequestBody SmsReSetPassWordDto reSetPassWordDto) {
        String phone = reSetPassWordDto.getPhone();
        int code = RandomCodeUtil.get6RandomCode();
        boolean flag = smsService.sendResetCode(phone, String.valueOf(code));
        if (!flag) {
            return new SmsExcution<Void>(null, SmsEnum.FALSE);
        }
        redisClient.save(String.valueOf(code), 5, CacheKeys.EMAIL_SEND_WECHAT_CODE, phone);
        return new SmsExcution<Void>(null,SmsEnum.SUCCESS);
    }
}
